package sui.utils.object;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * <p>
 * Description: 判断对象是否为空工具类
 * </p>
 * @author XQF SUI
 * @created 2016年12月1日 下午6:21:06
 * @version 1.0
 */
public class IsEmptyUtils {

    public static boolean isEmpty(Object o) {
        
        if (o == null) {
            return true;
        }
        
        if (o instanceof String) {
            return StringUtils.isBlank(o.toString());
        }
        
        if (o.getClass().isArray() && Array.getLength(o) == 0) {
            return true;
        }
        
        if (o instanceof Map && ((Map<?, ?>) o).isEmpty()) {
            return true;
        }
        
        if (o instanceof Collection<?> && CollectionUtils.isEmpty((Collection<?>) o)) {
            return true;
        }
        
        return false;
    }
    
    
    public static void main(String[] args) {
        
        Map<String, Object> map = new HashMap<String, Object>();
        /*map.put("name","张三");*/
        System.out.println("Map is empty: " + isEmpty(map));
/*        System.out.println(isEmpty(map.get("name")));
        System.out.println(isEmpty(map.get("sex")));*/
        
        String[] ids = {};
        
        System.out.println("String[] is empty: " + isEmpty(ids));
        
        Set<String> set = new HashSet<String>(); 
        
        System.out.println("Set is empty: " + isEmpty(set));
    }
}
